package com.hns.can.mainte.web;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.service.CommonInitParamService;
import com.hns.can.common.vo.CommonParamsVo;
import com.hns.can.common.web.BaseController;
import com.hns.can.mainte.entity.CommCarChargeInfoEntity;
import com.hns.can.mainte.service.ChargingMileAnalysisService;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.DateTool;

/**
 * 电池充电里程分析Controller
 * @author:zhangzq
 * @email:
 * @version Revision 2.0.0
 */
@RequestMapping("/chargingMileAnalysis")
@Controller
public class ChargingMileAnalysisController extends BaseController{
	
	final private String[] headerNames = new String[]{"车牌号","运营线路","所属机构","电池材料类型","上次充电时间","充电后SOC值(%)","当前充电时间","当前SOC值(%)","能耗消耗量(%)","运行里程(km)","平均车速(km/h)","本次充电后SOC值(%)","充电后续航里程(km)"};
	final private String[] headerFields = new String[]{"licPltNo","lineName","organName","bteryMaterialType","chargeEndTime","chargeEndSocValue","chargeBeginTime","chargeBeginSocValue","socDiffValue","totalMile","avgSpeed","chargeEndSocValue","durationMile"};
    
    @Autowired
    private ChargingMileAnalysisService chargingMileAnalysisService;
    @Autowired
    private CommonInitParamService commonInitParamService;
    
    /**
     * 分页查询充电里程分析
     * @param params
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/searchChargingMileAnalysis")
	@ResponseBody
    public Object searchChargingMileAnalysis(CommonParamsVo params) throws Exception {
    	SysUserEntity user = this.getCurrentUser().getUser();
    	commonInitParamService.initParamsLimitCar(params, user, Arrays.asList("T_COMM_CAR_CHARGE_INFO"));
    	Pagination page = chargingMileAnalysisService.searchChargingMileAnalysis(params);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
    
    /**
     * 导出充电里程分析Excel文件
     * @param params
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/exportChargingMileAnalysisExcel")
    public void exportChargingMileAnalysisExcel(CommonParamsVo params,HttpServletResponse response) throws Exception {
        List<CommCarChargeInfoEntity> list = getChargingMileAnalysisData(params);
        ExportUtil.exportFiles(Arrays.asList(headerNames), Arrays.asList(headerFields), list, response, "excel", "ChargingMileAnalysis"+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "充电里程分析数据");
    }
    
    /**
     * 导出充电里程分析Pdf文件
     * @param params
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/exportChargingMileAnalysisPdf")
    public void exportChargingMileAnalysisPdf(CommonParamsVo params,HttpServletResponse response) throws Exception {
		List<CommCarChargeInfoEntity> list = getChargingMileAnalysisData(params);
        ExportUtil.exportFiles(Arrays.asList(headerNames), Arrays.asList(headerFields), list, response, "pdf", "ChargingMileAnalysis"+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "充电里程分析数据");
    }
    
    /**
     * 获取充电里程分析数据用于导出
     * @param params
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    private List<CommCarChargeInfoEntity> getChargingMileAnalysisData(CommonParamsVo params) throws Exception {
    	SysUserEntity user = this.getCurrentUser().getUser();
    	commonInitParamService.initParamsLimitCar(params, user, Arrays.asList("T_COMM_CAR_CHARGE_INFO"));
        List<CommCarChargeInfoEntity> list = chargingMileAnalysisService.getChargingMileAnalysisData(params);
        return list;
    }
    
    /**
     * 获取电耗里程数据
     * @param params
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/getSocMileData")
	@ResponseBody
    public Object getSocMileData(CommonParamsVo params) throws Exception {
        List<CommCarChargeInfoEntity> list = chargingMileAnalysisService.getSocMileData(params);
        return this.getReturnResult(list);
    }
}
